<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.com/license/
 * @version    $Id: Controller.php 9747 2012-07-26 02:08:08Z john $
 * @author     John
 */

/**
 * @category   Application_Core
 * @package    User
 * @copyright  Copyright 2006-2010 Webligo Developments
 * @license    http://www.socialengine.com/license/
 */
class User_Widget_ProfileStoryFollowingController extends Engine_Content_Widget_Abstract
{
  protected $_childCount;
  
  public function indexAction()
  {

    // Don't render this if not authorized
    $viewer = Engine_Api::_()->user()->getViewer();
    if( !Engine_Api::_()->core()->hasSubject() ) {
      return $this->setNoRender();
    }

    // Get subject and check auth
    $subject = Engine_Api::_()->core()->getSubject('user');
    if( !$subject->authorization()->isAllowed($viewer, 'view') ) {
      return $this->setNoRender();
    }
    $this->view->viewer = $viewer;
    $this->view->subject = $subject;
    $own_profile = true;
    if($subject->getIdentity()!=$viewer->getIdentity()) $own_profile = false;
    $this->view->own_profile = $own_profile;

    $params = array(
      'published' => 1,
      'search' => 1,
      'follow' => $subject->getIdentity(),
      'page' => $this->_getParam('page', 1),
      'limit' => $this->_getParam('max', 5),
      'order' => $this->_getParam('order', 'recent'),
      'period' => $this->_getParam('period'),
      'user' => $this->_getParam('user'),
      'keyword' => $this->_getParam('keyword'),
      'category' => $this->_getParam('category'),
    );

    $paginator = Engine_Api::_()->story()->getStoriesPaginator($params);
    $params['follow'] = $viewer->getIdentity();
    $own_follow = Engine_Api::_()->story()->getStoriesPaginator($params);
    foreach($paginator as $row){
    	$followed[$row->story_id] = false;
    	foreach($own_follow as $own){
    		if($row->story_id==$own->story_id) {
    			$followed[$row->story_id] = true;
    			break;
	    	}
	    }
    }
    $this->view->followed = $followed;
    $this->view->paginator = $paginator;

    // Do not render if nothing to show
    if( $paginator->getTotalItemCount() <= 0 ) {
      return $this->setNoRender();
    }

    // Add count to title if configured
    if( $this->_getParam('titleCount', false) && $paginator->getTotalItemCount() > 0 ) {
      $this->_childCount = $paginator->getTotalItemCount();
    }
  }

  public function getChildCount()
  {
    return $this->_childCount;
  }
}